<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Profile extends MX_Controller {

	public function __construct() {
		parent::__construct();
		$this->template->set_partial('header_search', 'header/header_search');
		$this->template->set_partial('nav_bar', 'header/nav_bar');

		$this->load->model('member/member_model');
		$this->load->model('member/profile_model');
		$this->load->model('location/location_model');
		$this->load->model('category/category_model');
	}

	function add(){

		$data['type'] = $this->uri->segment(4);

		$this->template->set_layout('full_layout');
		$data['module_banner'] = FALSE;
		$data['module_recent_listing'] = TRUE;
		$data['module_news'] = FALSE;
		$data['member']['info'] = $this->member_model->getMember($this->session->userdata('mem_id'));

		$data['speciality'] = $this->profile_model->getSpecialities();



		$data['state'] = $this->profile_model->getStates();


		//--Profile Exist or Not --//
		$isProfile = $this->profile_model->getProfileByMemberID($this->session->userdata('mem_id'));
		if($isProfile){
			//$profile_id = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id'])->id;
			//$data['profile_id'] = $profile_id;
			$data['profile']['info'] = $isProfile;

			$data['city']= $this->location_model->get_city($data['profile']['info']->state);
			$data['payment'] = $this->profile_model->getPayment($data['profile']['info']->id);


		}else{
			$data['profile']['info'] = NULL;
			//$data['profile_id'] = null;
		}



		$data['plan'] = $this->profile_model->showlist();

		$this->template->build('profile',$data);
	}

	function edit(){


		$this->template->set_layout('full_layout');
		$data['module_banner'] = FALSE;
		$data['module_recent_listing'] = TRUE;
		$data['module_news'] = FALSE;
		$data['member']['info'] = $this->member_model->getMember($this->session->userdata('mem_id'));

		$data['speciality'] = $this->profile_model->getSpecialities();



		$data['state'] = $this->profile_model->getStates();


		//--Profile Exist or Not --//
		$isProfile = $this->profile_model->getProfileByMemberID($this->session->userdata('mem_id'));
		if($isProfile){
			$profile_id = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id'])->id;
			$data['profile_id'] = $profile_id;
			$data['profile']['info'] = $isProfile;


			//--GET SPECIALITY--//
			//$data['profile']['speciality']

			//--GET PAYMENT--//
			//$data['profile']['payment']
			$data['city']= $this->location_model->get_city($data['profile']['info']->state);
			$data['payment'] = $this->profile_model->getPayment($data['profile']['info']->id);


		}else{
			$data['profile']['info'] = NULL;
			$data['profile_id'] = null;
		}



		$data['plan'] = $this->profile_model->showlist();

		$this->template->build('profile',$data);
	}

	function submit($id=0){



		$id = $this->input->post('membership');

		$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
		$this->form_validation->set_rules('speciality[]', 'Speciality', 'trim|required|xss_clean');
		$this->form_validation->set_rules('payment[]','Payment','trim|required|xss_clean');
		//$this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean');
		$this->form_validation->set_rules('state', 'State/Province', 'trim|required|xss_clean');
		$this->form_validation->set_rules('city', 'City', 'trim|required|xss_clean');

		$this->form_validation->set_rules('zip', 'Zip Code', 'trim|required|xss_clean');


		$this->form_validation->set_rules('address1', 'Address Line 1 ', 'trim|required|xss_clean');
		$this->form_validation->set_rules('address2', 'Address Line 2 ', 'trim|xss_clean');

		$this->form_validation->set_rules('phone', 'Phone No (1)', 'trim|required|xss_clean');
		$this->form_validation->set_rules('phone2', 'Phone No (2) ', 'trim|xss_clean');

		$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_emails');
		$this->form_validation->set_rules('website', 'Website', 'trim|prep_url');

		$this->form_validation->set_rules('facebook_link','Facebook Link','trim|xss_clean');
		$this->form_validation->set_rules('twitter_link','Twitter Link','trim|xss_clean');
		$this->form_validation->set_rules('google_link','Google Link','trim|xss_clean');
		$this->form_validation->set_rules('linkedin_link','Linkedin Link','trim|xss_clean');

		$this->form_validation->set_rules('language','Spoken Language','trim|xss_clean|required');
		/*$this->form_validation->set_rules('cash','Cash','trim|xss_clean');
			$this->form_validation->set_rules('debitcard','Debitcard','trim|xss_clean');
		$this->form_validation->set_rules('creditcard','Credit Card','trim|xss_clean');
		$this->form_validation->set_rules('check','Check','trim|xss_clean');
		*/

		$this->form_validation->set_rules('consultation','Free Consultation','trim|xss_clean');
		$this->form_validation->set_rules('about','About you','trim|xss_clean|required|min_length[150]');

		if ($this->form_validation->run() == true) {
			$post_data = array(
					'mem_id' =>$this->session->userdata('mem_id'),
					'membership' => $id,
					'title' => trim($this->input->post('title')),
					//'country' => trim($this->input->post('country')),
					'state' => trim($this->input->post('state')),
					'city' => trim($this->input->post('city')),
					'zip' => trim($this->input->post('zip')),
					'address1' => trim($this->input->post('address1')),
					'address2' => trim($this->input->post('address2')),
					'phone1' => trim($this->input->post('phone')),
					'phone2' => trim($this->input->post('phone2')),

					'fax' => trim($this->input->post('fax')),
					'email' => trim($this->input->post('email')),
					'website' => trim($this->input->post('website')),

					'facebook' => $this->input->post('facebook_link'),
					'twitter' => $this->input->post('twitter_link'),
					'googleplus' => $this->input->post('google_link'),
					'linkedin' => $this->input->post('linkedin_link'),

					//'language' => trim($this->input->post('english')),
					//'language' => trim($this->input->post('french')),
					//'language' => trim($this->input->post('chinese')),
					'language' => trim($this->input->post('language')),
					'consultation' => $this->input->post('consultation'),
					'description' => $this->input->post('about'),
					'lat' => $this->input->post('lat'),
					'long' => $this->input->post('long')

			);

			//var_dump($post_data);
			//die();

			//--CHECK PROFILE ALREADY EXIST OR NOT --//
			$isProfile = $this->profile_model->getProfileByMemberID($this->session->userdata('mem_id'));
			if(!$isProfile){
				//-- INSERT INTO DATABASE --//
				$this->db->insert('profile',$post_data);

			}else{
				//-- UPDATE INTO DATABASE --//
				$this->db->where('id',$isProfile->id);
				$this->db->update('profile',$post_data);

			}

			//--Get Latest ID--//
			$bid = $this->_getLastID();


			//--SAVE SPECIALITY to TERM TABLE

			$sql = "Delete from term where listing_id=$bid and term='speciality'";
			$this->db->query($sql);

			//--SAVE SPECIALITY to TERM TABLE
			$speciality['list'] = $_POST['speciality'];
			//$speciality['list'] = $this->input->post('speciality');
			//$this->db->query("delete from term where listing_id = $bid and term='speciality'");
			foreach ($speciality['list'] as $key=>$value) {
				$post_data= array(
						'listing_id' =>$bid,
						'term_value	' => $value,
						'term' => 'key',
						'caption' => 'Keyword'
				);
				$this->db->insert('term',$post_data);
			}

			//--SAVE PAYMENT MODE to TERM TABLE
			$sql = "Delete from term where listing_id=$bid and term='payment'";
			$this->db->query($sql);
				
			$payment['list'] = $this->input->post('payment');
			foreach ($payment['list'] as $key=>$value) {
				$post_data= array(
						'listing_id' =>$bid,
						'term_value	' => $value,
						'term' => 'payment',
						'caption' => 'Payment'
				);
				$this->db->insert('term',$post_data);
			}




			$amt = $this->profile_model->get_membership($id)->price;
				
			$des = $this->profile_model->get_membership($id)->name;
				
			if(isset($id) && $amt!=0){
				//$amt=125.52;
				$u = 'member/payment/paypal/'.$amt.'/'.$bid;
				redirect($u);
			}else{
				$this->session->set_flashdata('message', 'Profile Updated Successfully');
				redirect('member/profile/edit');
			}
		}else{
			$this->session->set_flashdata('message', 'Invalid! Input Please check all the fields are entered correctly..');
			$this->add();
		}
	}
	
	function update(){

		$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
		$this->form_validation->set_rules('speciality[]', 'Speciality', 'trim|required|xss_clean');
		$this->form_validation->set_rules('payment[]','Payment','trim|xss_clean');
		//$this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean');
		$this->form_validation->set_rules('state', 'State/Province', 'trim|required|xss_clean');
		$this->form_validation->set_rules('city', 'City', 'trim|required|xss_clean');

		$this->form_validation->set_rules('zip', 'Zip Code', 'trim|required|xss_clean');


		$this->form_validation->set_rules('address1', 'Address Line 1 ', 'trim|required|xss_clean');
		$this->form_validation->set_rules('address2', 'Address Line 2 ', 'trim|xss_clean');

		$this->form_validation->set_rules('phone', 'Phone No (1)', 'trim|required|xss_clean');
		$this->form_validation->set_rules('phone2', 'Phone No (2) ', 'trim|xss_clean');

		$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_emails');
		$this->form_validation->set_rules('website', 'Website', 'trim|prep_url');

		$this->form_validation->set_rules('facebook_link','Facebook Link','trim|xss_clean');
		$this->form_validation->set_rules('twitter_link','Twitter Link','trim|xss_clean');
		$this->form_validation->set_rules('google_link','Google Link','trim|xss_clean');
		$this->form_validation->set_rules('linkedin_link','Linkedin Link','trim|xss_clean');

		$this->form_validation->set_rules('language','Spoken Language','trim|xss_clean|required');
		/*$this->form_validation->set_rules('cash','Cash','trim|xss_clean');
		 $this->form_validation->set_rules('debitcard','Debitcard','trim|xss_clean');
		$this->form_validation->set_rules('creditcard','Credit Card','trim|xss_clean');
		$this->form_validation->set_rules('check','Check','trim|xss_clean');
		*/

		$this->form_validation->set_rules('consultation','Free Consultation','trim|xss_clean');
		$this->form_validation->set_rules('about','About you','trim|xss_clean|required');

		if ($this->form_validation->run() == true) {
			$post_data = array(
					'mem_id' =>$this->session->userdata('mem_id'),
					'membership' => $this->input->post('membership'),
					'title' => trim($this->input->post('title')),
					//'country' => trim($this->input->post('country')),
					'state' => trim($this->input->post('state')),
					'city' => trim($this->input->post('city')),
					'zip' => trim($this->input->post('zip')),
					'address1' => trim($this->input->post('address1')),
					'address2' => trim($this->input->post('address2')),
					'phone1' => trim($this->input->post('phone')),
					'phone2' => trim($this->input->post('phone2')),

					'fax' => trim($this->input->post('fax')),
					'email' => trim($this->input->post('email')),
					'website' => trim($this->input->post('website')),

					'facebook' => $this->input->post('facebook_link'),
					'twitter' => $this->input->post('twitter_link'),
					'googleplus' => $this->input->post('google_link'),
					'linkedin' => $this->input->post('linkedin_link'),

					//'language' => trim($this->input->post('english')),
					//'language' => trim($this->input->post('french')),
					//'language' => trim($this->input->post('chinese')),
					'language' => trim($this->input->post('language')),
					'consultation' => $this->input->post('consultation'),
					'description' => $this->input->post('about'),
					'lat' => $this->input->post('lat'),
					'long' => $this->input->post('long')

			);

			//var_dump($post_data);
			//die();

			//--CHECK PROFILE ALREADY EXIST OR NOT --//
			$isProfile = $this->profile_model->getProfileByMemberID($this->session->userdata('mem_id'));
			if(!$isProfile){
				//-- INSERT INTO DATABASE --//
				$this->db->insert('profile',$post_data);

			}else{
				//-- UPDATE INTO DATABASE --//
				$this->db->where('id',$isProfile->id);
				$this->db->update('profile',$post_data);

			}

			//--Get Latest ID--//
			$bid = $this->_getLastID();


			//--SAVE SPECIALITY to TERM TABLE

			$sql = "Delete from term where listing_id = $bid and term='speciality'";
			$this->db->query($sql);

			//--SAVE SPECIALITY to TERM TABLE
			$speciality['list'] = $_POST['speciality'];
			//$speciality['list'] = $this->input->post('speciality');
			//$this->db->query("delete from term where listing_id = $bid and term='speciality'");
			foreach ($speciality['list'] as $key=>$value) {
				$post_data= array(
						'listing_id' =>$bid,
						'term_value	' => $value,
						'term' => 'key',
						'caption' => 'Keyword'
				);
				$this->db->insert('term',$post_data);
			}

			//--SAVE PAYMENT MODE to TERM TABLE
			$sql = "Delete from term where listing_id = $bid and term='payment'";
			$this->db->query($sql);

			$payment['list'] = $this->input->post('payment');
			foreach ($payment['list'] as $key=>$value) {
				$post_data= array(
						'listing_id' =>$bid,
						'term_value	' => $value,
						'term' => 'payment',
						'caption' => 'Payment'
				);
				$this->db->insert('term',$post_data);
			}
			$this->session->set_flashdata('message', 'Profile Updated Successfully');
			redirect('member/profile/edit/');
		}else{
			$this->session->set_flashdata('message', 'Invalid! Input Please check all the fields are entered correctly..');
			$this->edit();
		}
	}

	function _getLastID(){
		$mem_id = $this->session->userdata('mem_id');
		$sql = "Select max(id) as id from profile where mem_id = $mem_id ";
		$result= $this->db->query($sql)->row();
		if ($result)
		{
			return $result->id;
		}
	}
}
